package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.BxSdphdmx;
import com.ruoyi.system.mapper.BxSdphdMapper;
import com.ruoyi.system.domain.BxSdphd;
import com.ruoyi.system.service.IBxSdphdService;

/**
 * 智能配货单Service业务层处理
 * 
 * @author tianxingxia
 * @date 2025-03-22
 */
@Service
public class BxSdphdServiceImpl implements IBxSdphdService 
{
    @Autowired
    private BxSdphdMapper bxSdphdMapper;

    /**
     * 查询智能配货单
     * 
     * @param id 智能配货单主键
     * @return 智能配货单
     */
    @Override
    public BxSdphd selectBxSdphdById(Integer id)
    {
        return bxSdphdMapper.selectBxSdphdById(id);
    }

    /**
     * 查询智能配货单列表
     * 
     * @param bxSdphd 智能配货单
     * @return 智能配货单
     */
    @Override
    public List<BxSdphd> selectBxSdphdList(BxSdphd bxSdphd)
    {
        return bxSdphdMapper.selectBxSdphdList(bxSdphd);
    }

    /**
     * 新增智能配货单
     * 
     * @param bxSdphd 智能配货单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBxSdphd(BxSdphd bxSdphd)
    {
        bxSdphd.setCreateTime(DateUtils.getNowDate());
        int rows = bxSdphdMapper.insertBxSdphd(bxSdphd);
        insertBxSdphdmx(bxSdphd);
        return rows;
    }

    /**
     * 修改智能配货单
     * 
     * @param bxSdphd 智能配货单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBxSdphd(BxSdphd bxSdphd)
    {
        bxSdphd.setUpdateTime(DateUtils.getNowDate());
        bxSdphdMapper.deleteBxSdphdmxByDjid(bxSdphd.getId());
        insertBxSdphdmx(bxSdphd);
        return bxSdphdMapper.updateBxSdphd(bxSdphd);
    }

    /**
     * 批量删除智能配货单
     * 
     * @param ids 需要删除的智能配货单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBxSdphdByIds(Integer[] ids)
    {
        bxSdphdMapper.deleteBxSdphdmxByDjids(ids);
        return bxSdphdMapper.deleteBxSdphdByIds(ids);
    }

    /**
     * 删除智能配货单信息
     * 
     * @param id 智能配货单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBxSdphdById(Integer id)
    {
        bxSdphdMapper.deleteBxSdphdmxByDjid(id);
        return bxSdphdMapper.deleteBxSdphdById(id);
    }

    /**
     * 新增智能配货明细单信息
     * 
     * @param bxSdphd 智能配货单对象
     */
    public void insertBxSdphdmx(BxSdphd bxSdphd)
    {
        List<BxSdphdmx> bxSdphdmxList = bxSdphd.getBxSdphdmxList();
        Integer id = bxSdphd.getId();
        if (StringUtils.isNotNull(bxSdphdmxList))
        {
            List<BxSdphdmx> list = new ArrayList<BxSdphdmx>();
            for (BxSdphdmx bxSdphdmx : bxSdphdmxList)
            {
                bxSdphdmx.setDjid(id);
                list.add(bxSdphdmx);
            }
            if (list.size() > 0)
            {
                bxSdphdMapper.batchBxSdphdmx(list);
            }
        }
    }
}
